﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Data.SqlClient;
using DataAccess;

/// <summary>
/// Summary description for MSSQLDataAccess
/// </summary>
namespace DataAccess
{
    /// <summary>
    /// Summary description for DataProviderBase.
    /// </summary>
    public class DBDataProviderBase 
	{
        private string strKeyConnectionWebConfig = "ConnectionString";

        public DBDataProviderBase()
        {
        }
        //public ArrayList GetAll(string strSQL, Type t)
        //{	
        //    //MultiDB rewrite
        //    IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
            
        //    try
        //    {
        //        dbManager.Open();
        //        IDataReader reader = dbManager.ExecuteReader(CommandType.Text, strSQL);
        //        ArrayList kq = new ArrayList();
        //        kq = (ArrayList)CBO.FillCollection(reader, t);
        //        dbManager.Dispose();

        //        return kq;
        //    }
        //    catch (Exception ex)
        //    {
        //        dbManager.Dispose();
        //        return null;
        //    }
        //    //MultiDB rewrite
		   
        //}

        public DataSet ExecuteDataset(string strSQL)
        {
			//MultiDB rewrite
			IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
			//try
			{
				dbManager.Open();
				DataSet ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
				dbManager.Dispose();
				return ds;
			}
			//catch (System.Exception e)
			//{
			//    dbManager.Dispose();
			//    return null;
			//}
			
        }
        public DataSet ExecuteDataset(string strSQL, params object[] commands)
        {
			//MultiDB rewrite
			IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
			try
			{
				dbManager.CreateParameters(commands.Length);
				dbManager.Parameters = (IDbDataParameter[])commands;
				dbManager.Open();
				DataSet ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
				dbManager.Dispose();
				return ds;
			}
			catch (Exception ex)
			{
				dbManager.Dispose();
				return null;
			}
			
        }

        // Execute Procedute
        public DataSet ExecuteDatasetPro(string strSQL, params object[] commands)
        {
            //MultiDB rewrite
            IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
            try
            {
                dbManager.CreateParameters(commands.Length);
                dbManager.Parameters = (IDbDataParameter[])commands;
                dbManager.Open();
                DataSet ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, strSQL);
                dbManager.Dispose();
                return ds;
            }
            catch (Exception ex)
            {
                dbManager.Dispose();
                return null;
            }

        }

        //Recordset Page phai dung Dataset
        public DataSet ExecuteDataset(string strSQL, int starRecords, int maxRecords)
        {
        try
            {
                DBManager dbmg = new DBManager();
                dbmg.Open();
                string strConnection = dbmg.ConnectionString;
                SqlDataAdapter da = new SqlDataAdapter(strSQL, strConnection);
                DataSet ds = new DataSet();
                da.Fill(ds,starRecords, maxRecords, "SSS");
                da.Dispose();
                dbmg.Dispose();
                return ds;
            }
            catch (Exception ex)
            {
                
                return null;
            }

        }

        public DataRow ExecuteDataRow(string strSQL)
        {
			//MultiDB rewrite
			IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
			try
			{

				dbManager.Open();
				DataRow dr = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows[0];
				dbManager.Dispose();
				return dr;
			}
			catch (Exception ex)
			{
				dbManager.Dispose();
				return null;
			}
        }

        public IDataReader ExecuteReader(string strSQL)
        {
            //MultiDB rewrite
            IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
            try
            {

                dbManager.Open();
                IDataReader dr = dbManager.ExecuteReader(CommandType.Text, strSQL);
                dbManager.Dispose();
                return dr;
            }
            catch (Exception ex)
            {
                dbManager.Dispose();
                return null;
            }
        }

        //Execute Pro
        public IDataReader ExecuteReaderPro(string strSQL, params object[] commands)
        {
            //MultiDB rewrite
            IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
            try
            {
                dbManager.CreateParameters(commands.Length);
                dbManager.Parameters = (IDbDataParameter[])commands;
                dbManager.Open();
                IDataReader dr = dbManager.ExecuteReader(CommandType.StoredProcedure, strSQL);
                dbManager.Dispose();
                return dr;
            }
            catch (Exception ex)
            {
                dbManager.Dispose();
                return null;
            }
        }

		protected DataRow ExecuteDataRow(string strSQL, params object[] commands)
		{
			//MultiDB rewrite
			IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
			try
			{
				dbManager.CreateParameters(commands.Length);
				dbManager.Parameters = (IDbDataParameter[])commands;
				dbManager.Open();
				DataRow dr = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows[0];
				dbManager.Dispose();
				return dr;
			}
			catch (Exception ex)
			{
				dbManager.Dispose();
				return null;
			}
		}
        public bool ExecuteNonQuery(string strSQL, params object[] commands)
        {
			//MultiDB rewrite
			IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
			try
			{
				dbManager.CreateParameters(commands.Length);
				dbManager.Parameters = (IDbDataParameter[])commands;
				dbManager.Open();
				int retVal = dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
				
				dbManager.Dispose();
				return (retVal >= 0);
			}
			catch (Exception ex)
			{
				dbManager.Dispose();
				return false;
			}
			
        }

        //Execute proce
        public bool ExecuteNonQueryPro(string strSQL, params object[] commands)
        {
            //MultiDB rewrite
            IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
            try
            {
                dbManager.CreateParameters(commands.Length);
                dbManager.Parameters = (IDbDataParameter[])commands;
                dbManager.Open();
                int retVal = dbManager.ExecuteNonQuery(CommandType.StoredProcedure, strSQL);

                dbManager.Dispose();
                return (retVal >= 0);
            }
            catch (Exception ex)
            {
                dbManager.Dispose();
                return false;
            }

        }

        public bool ExecuteNonQuery(string strSQL)
        {
			//MultiDB rewrite
			IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
			try
			{
				dbManager.Open();
				int retVal = dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
				dbManager.Dispose();
				return (retVal >= 0);
			}
			catch (Exception ex)
			{
				dbManager.Dispose();
				return false;
			}
        }
        //protected object GetObject(string strSQL, Type t)
        //{
        //    //MultiDB rewrite
        //    IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
        //    try
        //    {
        //        dbManager.Open();
        //        IDataReader reader = dbManager.ExecuteReader(CommandType.Text, strSQL);
        //        object obj = CBO.FillObject(reader,t);
        //        dbManager.Dispose();
        //        return obj;
        //    }
        //    catch (Exception ex)
        //    {
        //        dbManager.Dispose();
        //        return null;
        //    }

        //}
        //protected object GetObject(string strSQL, Type t, params object[] commands)
        //{
        //    //MultiDB rewrite
        //    IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
        //    try
        //    {
        //        dbManager.CreateParameters(commands.Length);
        //        dbManager.Parameters = (IDbDataParameter[])commands;
        //        dbManager.Open();
        //        IDataReader reader = dbManager.ExecuteReader(CommandType.Text, strSQL);
        //        object obj = CBO.FillObject(reader, t);
        //        dbManager.Dispose();
        //        return obj;
        //    }
        //    catch (Exception ex)
        //    {
        //        dbManager.Dispose();
        //        return null;
        //    }

        //}
		
        public object ExecuteScalar(string strSQL)
        {
			//MultiDB rewrite
			IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
			try
			{
				dbManager.Open();
				object ob = dbManager.ExecuteScalar(CommandType.Text, strSQL);
				dbManager.Dispose();
				return ob;
			}
			catch (Exception ex)
			{
				dbManager.Dispose();
				return null;
			}
        }
		protected object ExecuteScalar(string strSQL, params object[] commands)
		{
			//MultiDB rewrite
			IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
			try
			{
				dbManager.CreateParameters(commands.Length);
				dbManager.Parameters = (IDbDataParameter[])commands;
				dbManager.Open();
				object ob = dbManager.ExecuteScalar(CommandType.Text, strSQL);
				dbManager.Dispose();
				return ob;
			}
			catch (Exception ex)
			{
				dbManager.Dispose();
				return null;
			}

			

		}

        //Execute Pro
        protected object ExecuteScalarPro(string strSQL, params object[] commands)
        {
            //MultiDB rewrite
            IDBManager dbManager = new DBManager(strKeyConnectionWebConfig, DBMSProvider.ProviderType);
            try
            {
                dbManager.CreateParameters(commands.Length);
                dbManager.Parameters = (IDbDataParameter[])commands;
                dbManager.Open();
                object ob = dbManager.ExecuteScalar(CommandType.StoredProcedure, strSQL);
                dbManager.Dispose();
                return ob;
            }
            catch (Exception ex)
            {
                dbManager.Dispose();
                return null;
            }



        }
      
    }//class
}
